import { _decorator, Component, Node, Button, Sprite, Label } from 'cc';
import { AudioMgr } from '../manager/AudioMgr';
import { AudioId } from '../../game/config/Config';
const { ccclass, property } = _decorator;

@ccclass('BaseButton')
export class BaseButton extends Button {

    @property({ tooltip: "按钮点击间隔时间" })
    touchTime: number = 1

    private touchmil: number = 0;
    private touched: boolean = false;
    _onTouchBegan(event) {
        if (!this.interactable || !this.enabledInHierarchy) return;
        super._onTouchBegan(event)
    }
    _onTouchEnded(event) {
        if (this.touched) {
            this.node.pauseSystemEvents(true)
            this._resetState();
            return;
        }
        AudioMgr.getInstance().playEffect(AudioId.btnClick);
        this.touched = true;
        this.touchmil = 0;
        super._onTouchEnded(event)
    }
    update(dt) {
        //触摸计时
        super.update(dt)
        if (this.touched) {
            //防止这个数加的很大
            this.touchmil = this.touchmil + dt;
            if (this.touchmil > this.touchTime) {
                this.touchmil = 0
                this.touched = false;
                this.node.resumeSystemEvents(true)
            }
        }
    }
    onDestroy() {
        this.touched = false;
    }
    onDisable() {
        super.onDisable()
        this.touched = false;
    }
    onEnable() {
        super.onEnable()
        this.touched = false;
    }
}
